Hệ thống quản lý thanh toán POS nhà hàng

1 Imports System.Data.OleDb
2 Imports System.Drawing.Printing
3 Public Class frmKitchen_Section
4     Dim st2 As String
5     Sub Reset()
6         cmbPrinter.SelectedIndex = -
1
7         chkIsEnabled.Checked = True
8         txtKitchenName.Text =
""
9         txtKitchenName.Focus()
10         btnSave.Enabled = True
11         btnUpdate.Enabled = False
12         btnDelete.Enabled = False
13     End Sub
14     Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
15         Me.Close()
16     End Sub
17     Private Sub PopulateInstalledPrintersCombo()
18         Try
19             
' Add list of installed printers found to the combo box.
20             
' The pkInstalledPrinters string will be used to provide the display string.
21             Dim i As Integer
22             Dim pkInstalledPrinters As String
23             cmbPrinter.Items.Clear()
24             For i =
0 To PrinterSettings.InstalledPrinters.Count - 1
25                 pkInstalledPrinters = PrinterSettings.InstalledPrinters.Item(i)
26                 cmbPrinter.Items.Add(pkInstalledPrinters)
27             Next
28         Catch ex As Exception
29             MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
30         End Try
31     End Sub
32     Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
33         If Len(Trim(txtKitchenName.Text)) =
0 Then
34             MessageBox.Show(
"Please enter kitchen/section name", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
35             txtKitchenName.Focus()
36             Exit Sub
37         End If
38         If Len(Trim(cmbPrinter.Text)) =
0 Then
39             MessageBox.Show(
"Please select printer", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
40             cmbPrinter.Focus()
41             Exit Sub
42         End If
43
44         Try
45             con = New OleDbConnection(cs)
46             con.Open()
47             Dim ct As String =
"select KitchenName from Kitchen where KitchenName=@d1"
48             cmd = New OleDbCommand(ct)
49             cmd.Connection = con
50             cmd.Parameters.AddWithValue(
"@d1", txtKitchenName.Text)
51             rdr = cmd.ExecuteReader()
52
53             If rdr.Read() Then
54                 MessageBox.Show(
"Kitchen/Section Name Already Exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
55                 txtKitchenName.Text =
""
56                 txtKitchenName.Focus()
57                 If (rdr IsNot Nothing) Then
58                     rdr.Close()
59                 End If
60                 Return
61             End If
62             If chkIsEnabled.Checked = True Then
63                 st2 =
"Yes"
64             Else
65                 st2 =
"No"
66             End If
67             con = New OleDbConnection(cs)
68             con.Open()
69             Dim cb As String =
"insert into Kitchen(KitchenName,Printer,IsEnabled) VALUES (@d1,@d2,@d3)"
70             cmd = New OleDbCommand(cb)
71             cmd.Connection = con
72             cmd.Parameters.AddWithValue(
"@d1", txtKitchenName.Text)
73             cmd.Parameters.AddWithValue(
"@d2", cmbPrinter.Text)
74             cmd.Parameters.AddWithValue(
"@d3", st2)
75             cmd.ExecuteReader()
76             con.Close()
77             Dim st As String =
"added the new Kitchen/section '" & txtKitchenName.Text & "'"
78             LogFunc(lblUser.Text, st)
79             MessageBox.Show(
"Successfully saved", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
80             btnSave.Enabled = False
81             Getdata()
82             Reset()
83         Catch ex As Exception
84             MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
85         End Try
86     End Sub
87
88     Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
89         Try
90             If MessageBox.Show(
"Do you really want to delete this record?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
91                 DeleteRecord()
92             End If
93         Catch ex As Exception
94             MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
95         End Try
96     End Sub
97     Private Sub DeleteRecord()
98
99         Try
100             con.Open()
101             con = New OleDbConnection(cs)
102             con.Open()
103             Dim cl As String =
"select Kitchen.KitchenName from Kitchen,Dish where Kitchen.KitchenName=Dish.Kitchen and Kitchen.KitchenName=@d1"
104             cmd = New OleDbCommand(cl)
105             cmd.Connection = con
106             cmd.Parameters.AddWithValue(
"@d1", txtKitchenName.Text)
107             rdr = cmd.ExecuteReader()
108             If rdr.Read Then
109                 MessageBox.Show(
"Unable to delete..Already in use in Menu Item Entry", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
110                 If Not rdr Is Nothing Then
111                     rdr.Close()
112                 End If
113                 Exit Sub
114             End If
115             con.Close()
116             Dim RowsAffected As Integer =
0
117             con = New OleDbConnection(cs)
118             con.Open()
119             Dim cq As String =
"delete from Kitchen where KitchenName=@d1"
120             cmd = New OleDbCommand(cq)
121             cmd.Connection = con
122             cmd.Parameters.AddWithValue(
"@d1", txtKitchenName.Text)
123             RowsAffected = cmd.ExecuteNonQuery()
124             If RowsAffected >
0 Then
125                 Dim st As String =
"deleted the Kitchen/section '" & txtKitchenName.Text & "'"
126                 LogFunc(lblUser.Text, st)
127                 MessageBox.Show(
"Successfully deleted", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
128                 Getdata()
129                 Reset()
130             Else
131                 MessageBox.Show(
"No Record found", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Information)
132                 Reset()
133             End If
134             If con.State = ConnectionState.Open Then
135                 con.Close()
136
137             End If
138         Catch ex As Exception
139             MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
140         End Try
141     End Sub
142
143     Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
144         If Len(Trim(txtKitchenName.Text)) =
0 Then
145             MessageBox.Show(
"Please enter kitchen/section name", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
146             txtKitchenName.Focus()
147             Exit Sub
148         End If
149         If Len(Trim(cmbPrinter.Text)) =
0 Then
150             MessageBox.Show(
"Please select printer", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
151             cmbPrinter.Focus()
152             Exit Sub
153         End If
154
155         Try
156             If chkIsEnabled.Checked = True Then
157                 st2 =
"Yes"
158             Else
159                 st2 =
"No"
160             End If
161             con = New OleDbConnection(cs)
162             con.Open()
163             Dim cb As String =
"Update Kitchen set KitchenName=@d1,Printer=@d2,IsEnabled=@d3 where KitchenName=@d4"
164             cmd = New OleDbCommand(cb)
165             cmd.Connection = con
166             cmd.Parameters.AddWithValue(
"@d1", txtKitchenName.Text)
167             cmd.Parameters.AddWithValue(
"@d2", cmbPrinter.Text)
168             cmd.Parameters.AddWithValue(
"@d3", st2)
169             cmd.Parameters.AddWithValue(
"@d4", txtKitchen.Text)
170             cmd.ExecuteReader()
171             con.Close()
172             Dim st As String =
"Updated the Kitchen/section '" & txtKitchenName.Text & "'"
173             LogFunc(lblUser.Text, st)
174             MessageBox.Show(
"Successfully updated", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
175             btnUpdate.Enabled = False
176             Getdata()
177         Catch ex As Exception
178             MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
179         End Try
180     End Sub
181     Public Sub Getdata()
182         Try
183             con = New OleDbConnection(cs)
184             con.Open()
185             cmd = New OleDbCommand(
"SELECT RTRIM(KitchenName), RTRIM(Printer),RTRIM(IsEnabled) from Kitchen order by KitchenName", con)
186             rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
187             dgw.Rows.Clear()
188             While (rdr.Read() = True)
189                 dgw.Rows.Add(rdr(
0), rdr(1), rdr(2))
190             End While
191             con.Close()
192         Catch ex As Exception
193             MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
194         End Try
195     End Sub
196     Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
197         Reset()
198     End Sub
199
200     Private Sub frmKitchen_Section_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
201         If e.KeyCode = Keys.Enter Then
202             Me.SelectNextControl(Me.ActiveControl, True, True, True, False)
'for Select Next Control
203         End If
204     End Sub
205
206     Private Sub frmKitchen_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
207         Getdata()
208         PopulateInstalledPrintersCombo()
209     End Sub
210
211     Private Sub dgw_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles dgw.MouseClick
212         Try
213             If dgw.Rows.Count >
0 Then
214                 Dim dr As DataGridViewRow = dgw.SelectedRows(
0)
215                 txtKitchenName.Text = dr.Cells(
0).Value.ToString()
216                 txtKitchen.Text = dr.Cells(
0).Value.ToString()
217                 cmbPrinter.Text = dr.Cells(
1).Value.ToString()
218                 If dr.Cells(
2).Value.ToString() = "Yes" Then
219                     chkIsEnabled.Checked = True
220                 Else
221                     chkIsEnabled.Checked = False
222                 End If
223                 btnUpdate.Enabled = True
224                 btnDelete.Enabled = True
225                 btnSave.Enabled = False
226             End If
227         Catch ex As Exception
228             MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
229         End Try
230     End Sub
231
232     Private Sub dgw_RowPostPaint(sender As Object, e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles dgw.RowPostPaint
233         Dim strRowNumber As String = (e.RowIndex +
1).ToString()
234         Dim size As SizeF = e.Graphics.MeasureString(strRowNumber, Me.Font)
235         If dgw.RowHeadersWidth < Convert.ToInt32((size.Width +
20)) Then
236             dgw.RowHeadersWidth = Convert.ToInt32((size.Width +
20))
237         End If
238         Dim b As Brush = SystemBrushes.ControlText
239         e.Graphics.DrawString(strRowNumber, Me.Font, b, e.RowBounds.Location.X +
15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2))
240
241     End Sub
242 End Class


Gõ tìm kiếm nhanh...